package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.SystemMenu;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.result.MenuResult;
import cn.wolfcode.cms.service.ISystemMenuService;
import cn.wolfcode.cms.util.JSONResult;
import cn.wolfcode.cms.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {

    @Autowired
    private ISystemMenuService systemMenuService;


    //菜单首页
    @RequestMapping("")
    @RequiresPermissions("systemMenu:index")
    @PermissionName("菜单首页")
    public String index() {
        return "systemMenu/list";
    }


    //菜单数据列表
    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("systemMenu:list")
    @PermissionName("菜单列表")
    public Object list(QueryObject qo) {
        PageResult result = systemMenuService.query(qo);
        return result;
    }

    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {
        List<SystemMenu> list = systemMenuService.getParentMenus();
        return list;
    }

    @RequestMapping("listRoot")
    @ResponseBody
    public Object listRoot() {
        List<SystemMenu> list = systemMenuService.listRoot();
        MenuResult result = new MenuResult();
        result.setData(list);
        return result;
    }


    //菜单添加
    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("systemMenu:save")
    @PermissionName("菜单添加")
    public Object save(SystemMenu entity) {
        JSONResult result = new JSONResult();
        try {
            systemMenuService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //菜单编辑
    @RequestMapping("edit")
    @ResponseBody
    @RequiresPermissions("systemMenu:edit")
    @PermissionName("菜单编辑")
    public Object edit(SystemMenu entity) {
        JSONResult result = new JSONResult();
        try {
            systemMenuService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }


    //菜单删除
    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("systemMenu:delete")
    @PermissionName("菜单删除")
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            systemMenuService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }


}
